<!--
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-04-21 10:05:44
 * @LastEditTime: 2022-04-21 10:59:12
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description: 适配器模式
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>适配器模式</title>
</head>

<body>
  <!-- 地图数据渲染 -->
</body>
<script>
  /*  
  原始数据: [
    { "name": "东城", "id": 1 }, 
    { "name": "西城", "id": 2 },
    { "name": "朝阳", "id": 3 }, 
    { "name": "丰台", "id": 4 }
  ]

   适配数据:{ "东城": 1, "西城": 2, "朝阳": 3, "丰台": 4 } 
  */
  const getBeijingCity = function () {
    const beijingCity = [
      { name: '东城', id: 1 },
      { name: '西城', id: 2 },
      { name: '朝阳', id: 3 },
      { name: '丰台', id: 4 },
    ]
    return beijingCity
  }

  const render = function (fn) {
    console.log(JSON.stringify(fn()));
  }

  // 适配器
  const addressAdapter = function (oldAddress) {
    let address = {},
      oldAddressData = oldAddress(),
      item = {},
      length = oldAddressData.length

    for (let i = 0; i < length; i++) {
      item = oldAddressData[i]
      address[item.name] = item.id
    }
    return function () {
      return address
    }
  }

  render(getBeijingCity)
  render(addressAdapter(getBeijingCity))

</script>

</html>